<?php
/**
 * Created by PhpStorm.
 * User: 小灰灰
 * Date: 2023-11-07
 * Time: 11:16:46
 * Info: 友情链接模型
 */

namespace app\common\model;

class Links extends TimeInit
{

    /**
     * 获取数量
     * @return int
     */
    public static function getCount(): int
    {
        $count = self::where(['status' => 1])->count();

        return $count;
    }

    /**
     * 获取数据列表
     * @return array
     */
    public static function getList(int $page = 1, int $limit = 10, string $order = "sort asc", $where = ""): array
    {
        $first = ($page - 1) * $limit;
        $list  = self::field('g.title as group_name,l.*')->alias('l')->leftJoin('links_group g',
            'l.group_id=g.id')->where(['l.status' => 1])->where(function ($query) use ($where) {
            if ( ! empty($where)) {
                $query->where($where);
            }
        })->limit($first, $limit)->order($order)->select()->toArray();
        foreach ($list as $key => $v) {
            $list[$key]['target'] = isset($v['target']) && $v['target'] == 1 ? "_blank" : "";
        }

        return $list;
    }
}
